package com.lzc.product.controller;

import com.lzc.product.dataobject.Product;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class ProductController {

    public static ConcurrentHashMap<Integer, Product> products = new ConcurrentHashMap<>();

    public static AtomicInteger count = new AtomicInteger(0);

    @GetMapping("/products")
    public Collection<Product> products() {
        return products.values();
    }

    @PostMapping("/products")
    public Product addProduct(Product product) {
        Integer id = count.addAndGet(1);
        product.setProductId(id);
        products.put(id, product);
        return product;
    }

    @GetMapping("/products/{id}")
    public Product getProductById(@PathVariable Integer id) {
        return products.get(id);
    }

    @DeleteMapping("/products/{id}")
    public Product deleteProductById(@PathVariable Integer id) {
        return products.remove(id);
    }
}
